#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

#define red 0
#define blue 1

int main()
{
    int color_flag;
    Mat srcImage;
    Mat showImage;
    Mat midImage;
    Mat binImage;
    vector<Mat> channels;
    VideoCapture cap("../video/red.avi");
    color_flag = red;

while(1)
{
    Point2f temp[4];
    RotatedRect rect_s,rect_b;
    if(!cap.read(srcImage)) break;
    GaussianBlur(srcImage, showImage, Size(3,3), 0);        
    split(showImage, channels);
    if(color_flag == red)
    {
        midImage = channels.at(2) - channels.at(0);
    }else
    {
        midImage = channels.at(0) - channels.at(2);
    }
    double th = threshold(midImage, binImage, 40, 255, THRESH_BINARY|THRESH_OTSU);   
    if(th-10 > 0)
    {
        threshold(midImage,binImage, th-10, 255, THRESH_BINARY);
    }
    vector<Rect> rects_buf;
    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;
    findContours(binImage,contours,hierarchy,RETR_CCOMP,CHAIN_APPROX_NONE);
    int contour_index = 0;
    int min_contour_Area = 100000000;
    for(int i = 0; i < contours.size(); i++)
    {
        //第一步找到所有的小方框，小矩形
        if(hierarchy[i][3]<0 || contours[i].size() < 6 || contours[static_cast<uint>(hierarchy[i][3])].size() < 6)
            continue;
        double small_rect_area = contourArea(contours[i]);
        double small_rect_length = arcLength(contours[i],true);
        if(small_rect_length < 10)
            continue;

        //找到所有小方框，获得其对应的大方框
        Rect rect = boundingRect(contours[static_cast<uint>(hierarchy[i][3])]);
        rects_buf.push_back(rect);
        if(small_rect_area < 200)
            continue;
        double big_rect_area = contourArea(contours[static_cast<uint>(hierarchy[i][3])]);
        double big_rect_length = arcLength(contours[static_cast<uint>(hierarchy[i][3])],true);

        //用大方框的条件再做一次判断
        if(big_rect_area < 300)
            continue;
        if(big_rect_length < 50)
            continue;
        // debug用
        // drawContours(tempImage, contours, i, Scalar(255), 1);
        float contour_Area = contourArea(contours[static_cast<uint>(hierarchy[i][3])]);

        //得到的旋转小矩形，与其对应的父亲旋转矩形
        rect_s = minAreaRect(contours[i]);
        rect_b = minAreaRect(contours[static_cast<uint>(hierarchy[i][3])]);

        Point2f pts_Rect_s[4], pts_Rect_b[4];
        rect_s.points(pts_Rect_s);rect_b.points(pts_Rect_b);
        // 用来标记所有父轮廓
        // for(int i = 0; i < 4; i++)
        // {
        //      line(showImage, pts_Rect_b[i], pts_Rect_b[(i+1) % 4], Scalar(0,255,255), 2, 8);
        //      line(tempImage, pts_Rect_s[i], pts_Rect_s[(i+1) % 4], Scalar(123,234,212), 2, 8);
        // }
        // 查找最小父轮廓
        if(i == 0)
        {
            //初始化查找变量
            min_contour_Area = contour_Area;
            contour_index = i;
        }else
        {
            if(contour_Area < min_contour_Area)
            {
                min_contour_Area = contour_Area;
                contour_index = i;
            }
        }
    }
    // 找到最小面积父轮廓的子轮廓
    minAreaRect(contours[contour_index]).points(temp);
    if(rect_s.center.x != 0 && rect_s.center.y != 0)
    {
        for(int i = 0; i < 4; i++)
        {
            line(showImage, temp[i], temp[(i+1) % 4], Scalar(0,255,255), 2, 8);
        }
        line(showImage, temp[0], temp[2], Scalar(255,255,255), 1);
        line(showImage, temp[1], temp[3], Scalar(255,255,255), 1);
        short hit_point_x = minAreaRect(contours[contour_index]).center.x;
        short hit_point_y = minAreaRect(contours[contour_index]).center.y;
        circle(showImage, Point(hit_point_x, hit_point_y), 2, Scalar(255,0,255), -1);
    }
    imshow("finally",showImage);
    waitKey(40);
}
}